<?php
  if (!defined('XOOPS_ROOT_PATH')) { exit(); }
  
  $limit = (!empty($_GET['limit'])) ? $_GET['limit'] : 30;
  $start = (!empty($_GET['start'])) ? $_GET['start'] : 0;
  $polls_arr =& XoopsPoll::getAll(array(), true, "weight ASC, end_time DESC", $limit+1, $start);
  $polls_count = count($polls_arr);
  
  $html = '<h4>'._AM_POLLCONF.'</h4><h4 style="text-align:left;">'._AM_POLLSLIST.'</h4>';
  if ( is_array($polls_arr) && $polls_count > 0) {
    $html.= '<form action="index.php" method="post">
    <table class="outer">
    <tr><th>'._AM_DISPLAYBLOCK.'</th><th>'._AM_DISPLAYORDER.'</th><th>'._AM_POLLQUESTION.'</th>
    <th>'._AM_VOTERS.'</th><th>'._AM_VOTES.'</th><th>'._AM_EXPIRATION.'</th><th>&nbsp;</th></tr>';
    $max = ( $polls_count > $limit ) ? $limit : $polls_count;
    for ( $i = 0; $i < $max; $i++ ) {
      $checked = "";
      if ( 1 == $polls_arr[$i]->getVar('display') ) {
        $checked = ' checked="checked"';
      }
      if ( $polls_arr[$i]->getVar('end_time') > time() ) {
        $end = formatTimestamp($polls_arr[$i]->getVar('end_time'),'m');
      } else {
        $end = '<span style="color:#ff0000;">'._AM_EXPIRED.'</span><br /><a href="index.php?op=restart&amp;poll_id='.$polls_arr[$i]->getVar('poll_id').'">'._AM_RESTART.'</a>';
      }
      $html.= '<tr class="odd"><td align="center"><input type="hidden" name="poll_id['.$i.']" value="'.$polls_arr[$i]->getVar('poll_id').'" />
      <input type="hidden" name="old_display['.$i.']" value="'.$polls_arr[$i]->getVar('display').'" />
      <input type="checkbox" name="display['.$i.']" value="1"'.$checked.' /></td>
      <td><input type="hidden" name="old_weight['.$i.']" value="'.$polls_arr[$i]->getVar('weight').'" />
      <input type="text" name="weight['.$i.']" value="'.$polls_arr[$i]->getVar('weight').'" size="6" maxlength="5" /></td>
      <td>'.$polls_arr[$i]->getVar('question').'</td><td align="center">'.$polls_arr[$i]->getVar('voters').'</td>
      <td align="center">'.$polls_arr[$i]->getVar('votes').'</td><td>'.$end.'</td>
      <td align="right"><a href="index.php?op=edit&amp;poll_id='.$polls_arr[$i]->getVar('poll_id').'">'._EDIT.'</a><br />
      <a href="index.php?op=delete&amp;poll_id='.$polls_arr[$i]->getVar('poll_id').'">'._DELETE.'</a><br />
      <a href="index.php?op=log&amp;poll_id='.$polls_arr[$i]->getVar('poll_id').'">'._AM_VIEWLOG.'</a></td></tr>';
    }
    $html.= '<tr align="right" class="even">
    <td colspan="7"><input type="button" class="formButton" name="button" onclick="location=\'index.php?op=add\'" value="'._AM_ADDPOLL.'" />
    <input type="submit" class="formButton" value="'._SUBMIT.'" /><input type="hidden" name="op" value="quickupdate" /></td></tr></table>
    </form>';
    $html.= '<table width="100%"><tr><td align="left">';
    if ( $start > 0 ) {
      $prev_start = ($start - $limit > 0) ? $start - $limit : 0;
      $html.= '<a href="index.php?start='.$prev_start.'&amp;limit='.$limit.'">'._PL_PREV.'</a>';
    } else {
      $html.= '&nbsp;';
    }
    $html.= '</td><td align="right">';
    if ( $polls_count > $limit ) {
      $html.= '<a href="index.php?start='.($start+$limit).'&amp;limit='.$limit.'">'._PL_NEXT.'</a>';
    }
    $html.= '</td></tr></table>';
  }
  
  xoops_cp_header();
  echo $html;
  xoops_cp_footer();
?>